/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.personal.controller;

import com.alibaba.fastjson.JSONArray;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.personal.domain.UserWorkbench;
import com.koron.personal.service.api.UserWorkbenchService;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import com.koron.system.utils.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Api(tags = "userWorkbench", description = "用户工作台接口类")
@RestController
@RequestMapping("/userWorkbench")
public class UserWorkbenchController {

    @Autowired
    private UserWorkbenchService service;

    @ApiOperation("保存工作台信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userWorkbenchs", value = "用户工作台json字符串", dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/save.htm", method = RequestMethod.POST)
    @Log(title = "用户工作台管理", businessModule = "用户工作台", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<Integer> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String userWorkbenchs) {
        MessageBean<Integer> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NOT_LOGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return msg;
        }
        List<UserWorkbench> userWorkbenchList = new ArrayList<>();
        if (StringUtils.isNotEmpty(userWorkbenchs)) {
            userWorkbenchList = JSONArray.parseArray(userWorkbenchs, UserWorkbench.class);
        }
        msg = service.save(user, userWorkbenchList);
        return msg;
    }


    @ApiOperation("查询用户的工作台信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "用户code:", dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public MessageBean<List> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String userCode) {
        if (null == user) {
            MessageBean<List> msg = new MessageBean();
            msg.setCode(BusinessCode.NOT_LOGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return msg;
        }
        MessageBean<List> msg = service.list(user);
        return msg;
    }

}
